package com.beiing.tianshuai.tianshuai.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.app.HttpRequestConstant;
import com.beiing.tianshuai.tianshuai.base.BaseRecyclerViewAdapter;
import com.beiing.tianshuai.tianshuai.entity.NewFriendMessageBean;
import com.beiing.tianshuai.tianshuai.widget.CircleImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bingoogolapple.bgabanner.BGAOnNoDoubleClickListener;

/**
 * Created by Jinni on 2017/8/8 0008.
 */

public class NewFriendAdapter extends BaseRecyclerViewAdapter<NewFriendMessageBean.DataBean.NumberBean> {

    private Context mContext;
    private OnItemClickListener mListener;

    public void setListener(OnItemClickListener listener) {
        mListener = listener;
    }

    public NewFriendAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder;
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_new_friend_message, parent, false);
        holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            final NewFriendMessageBean.DataBean.NumberBean bean = mList.get(position);
            String status = bean.getStatus();
            //0--请求中;1--请求通过;2--拒绝请求;3--操作过期
            if ("0".equals(bean.getFriendstype())) {
                ((ViewHolder) holder).mItem.setOnClickListener(new BGAOnNoDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View v) {
                        mListener.onItemClick(bean.getId(), bean.getFormUid(), bean.getToUid());
                    }
                });
                switch (status) {
                    case "0":
                        ((ViewHolder) holder).mBtnAgree.setVisibility(View.VISIBLE);
                        ((ViewHolder) holder).mBtnAgree.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                mListener.onAgreeClick(bean.getId(), bean.getFormUid(), bean.getToUid());
                            }
                        });
                        ((ViewHolder) holder).mBtnRefuse.setVisibility(View.VISIBLE);
                        ((ViewHolder) holder).mBtnRefuse.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                mListener.onRefuseClick(bean.getId(), bean.getFormUid(), bean.getToUid());
                            }
                        });
                        ((ViewHolder) holder).mBtnStatus.setVisibility(View.GONE);
                        break;
                    case "1":
                        ((ViewHolder) holder).mBtnAgree.setVisibility(View.GONE);
                        ((ViewHolder) holder).mBtnRefuse.setVisibility(View.GONE);
                        ((ViewHolder) holder).mBtnStatus.setVisibility(View.VISIBLE);
                        ((ViewHolder) holder).mBtnStatus.setText("已同意");
                        break;
                    case "2":
                        ((ViewHolder) holder).mBtnAgree.setVisibility(View.GONE);
                        ((ViewHolder) holder).mBtnRefuse.setVisibility(View.GONE);
                        ((ViewHolder) holder).mBtnStatus.setVisibility(View.VISIBLE);
                        ((ViewHolder) holder).mBtnStatus.setText("已拒绝");
                        break;
                    case "3":
                        ((ViewHolder) holder).mBtnAgree.setVisibility(View.GONE);
                        ((ViewHolder) holder).mBtnRefuse.setVisibility(View.GONE);
                        ((ViewHolder) holder).mBtnStatus.setVisibility(View.VISIBLE);
                        ((ViewHolder) holder).mBtnStatus.setText("请求已过期");
                        break;
                }
            } else {
                ((ViewHolder) holder).mItem.setOnClickListener(new BGAOnNoDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View v) {
                        mListener.onItemClick(bean.getId(), bean.getToUid(), bean.getFormUid());
                    }
                });
                switch (status) {
                    case "0":
                        ((ViewHolder) holder).mBtnAgree.setVisibility(View.GONE);
                        ((ViewHolder) holder).mBtnRefuse.setVisibility(View.GONE);
                        ((ViewHolder) holder).mBtnStatus.setVisibility(View.VISIBLE);
                        ((ViewHolder) holder).mBtnStatus.setText("已发送等待验证");
                        break;
                    case "1":
                        ((ViewHolder) holder).mBtnAgree.setVisibility(View.GONE);
                        ((ViewHolder) holder).mBtnRefuse.setVisibility(View.GONE);
                        ((ViewHolder) holder).mBtnStatus.setVisibility(View.VISIBLE);
                        ((ViewHolder) holder).mBtnStatus.setText("对方已同意");
                        break;
                    case "2":
                        ((ViewHolder) holder).mBtnAgree.setVisibility(View.GONE);
                        ((ViewHolder) holder).mBtnRefuse.setVisibility(View.GONE);
                        ((ViewHolder) holder).mBtnStatus.setVisibility(View.VISIBLE);
                        ((ViewHolder) holder).mBtnStatus.setText("对方已拒绝");
                        break;
                    case "3":
                        ((ViewHolder) holder).mBtnAgree.setVisibility(View.GONE);
                        ((ViewHolder) holder).mBtnRefuse.setVisibility(View.GONE);
                        ((ViewHolder) holder).mBtnStatus.setVisibility(View.VISIBLE);
                        ((ViewHolder) holder).mBtnStatus.setText("请求已过期");
                        break;
                }
            }
            RequestOptions avatarOptions = new RequestOptions()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .placeholder(R.mipmap.default_avatar)
                    .centerCrop()
                    .override(100, 100);
            Glide.with(mContext.getApplicationContext())
                    .load(TextUtils.isEmpty(
                            bean.getPhoto()) ?
                            R.mipmap.default_avatar :
                            HttpRequestConstant.USER_PIC_HEAD + bean.getPhoto())
                    .apply(avatarOptions)
                    .into(((ViewHolder) holder).mCivUserAvatar);
            ((ViewHolder) holder).mTvUserNickname.setText(bean.getName());
            ((ViewHolder) holder).mTvUserMessage.setText(bean.getContent());
            String sex = bean.getSex();
            switch (TextUtils.isEmpty(sex) ? "1" : sex) {
                case "1":
                    Drawable drawableBoy = mContext.getResources().getDrawable(R.drawable.icon_boy_blue);
                    drawableBoy.setBounds(0, 0, drawableBoy.getMinimumWidth(), drawableBoy.getMinimumHeight());
                    ((ViewHolder) holder).mTvUserNickname.setCompoundDrawables(null, null, drawableBoy, null);
                    break;
                case "2":
                    Drawable drawableGirl = mContext.getResources().getDrawable(R.drawable.icon_girl_pink);
                    drawableGirl.setBounds(0, 0, drawableGirl.getMinimumWidth(), drawableGirl.getMinimumHeight());
                    ((ViewHolder) holder).mTvUserNickname.setCompoundDrawables(null, null, drawableGirl, null);
                    break;
            }
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private View mItem;
        @BindView(R.id.civ_user_avatar)
        CircleImageView mCivUserAvatar;
        @BindView(R.id.tv_user_nickname)
        TextView mTvUserNickname;
        @BindView(R.id.tv_user_message)
        TextView mTvUserMessage;
        @BindView(R.id.btn_status)
        TextView mBtnStatus;
        @BindView(R.id.btn_refuse)
        TextView mBtnRefuse;
        @BindView(R.id.btn_agree)
        TextView mBtnAgree;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            mItem = itemView;
        }
    }

    public interface OnItemClickListener {
        void onAgreeClick(String id, String fromId, String toId);
        void onRefuseClick(String id, String fromId, String toId);
        void onItemClick(String id, String fromId, String toId);
    }
}
